'use strict'

module.exports = function(grunt) {
  require('load-grunt-tasks')(grunt);

  require('time-grunt')(grunt);
  grunt.loadNpmTasks('grunt-contrib-requirejs');

  var config = {
    dist: 'dist'
  };
  var path = require('path');
  grunt.initConfig({
    config: config,
    sprite: {
      all: {
        src: 'images/page/*.png',
        dest: 'images/page.png',
        destCss: 'sass/page.css',
        cssTemplate: 'handlebars/sprite.handlebars',
        padding: 20
      }
    },
    clean: {
      server: 'dist',
    },
    concat: {
      options: {
        separator: ';',
        stripBanners: true
      },
      dist: {
        src: ['js/lib/*.js'
        ],
        dest: "js/build.js"
      }
    },
    uglify: {
      options: {},
      dist: {
        files: {
          'jsbridge.min.js': [
            'jsbridge.js'
          ]
        }
      }
    },
    cssmin: {
      options: {
        keepSpecialComments: 0
      },
      compress: {
        files: [{
          expand: true,
          cwd: '.tmp/css/',
          src: ['{,*/}*.css'],
          dest: 'dist/css',
          ext: '.css'
        }]
      }
    },

    copy: {
      styles: {
        expand: true,
        dot: true,
        cwd: '.tmp/css',
        dest: 'dist/css/',
        src: '{,*/}*.css'
      },
      dist: {
        files: [{
          expand: true,
          dot: true,
          // cwd: 'html',
          dest: 'dist/',
          src: 'index.html'
        }, {
          expand: true,
          dot: true,
          cwd: 'js/',
          dest: 'dist/js/',
          src: '*.js'
        }, {
          expand: true,
          dot: true,
          cwd: 'img/',
          dest: 'dist/img/',
          src: '{*.png,*.jpg}'
        },{
          expand:true,
          dot: true,
          dest: 'dist/',
          src: '*.mp3'
        }]


      },


    },
    imagemin: {
      dist: {
        files: [{
          expand: true,
          cwd: 'images',
          src: '{,*/}*.png',
          dest: 'dist/images'
        }]
      }
    },
    // Renames files for browser caching purposes
    filerev: {
      dist: {
        src: [
          'dist/js/{,*/}*.js',
          'dist/css/{,*/}*.css',
          // 'test/index.html',
        ]
      }
    },

    watch: {

      js: {
        files: ['scripts/{,*/}*.js'],
        options: {
          livereload: true
        }
      },
      gruntfile: {
        files: ['Gruntfile.js']
      },
      sass: {
        files: ['sass/{,*/}*.{scss,sass}'],
        tasks: ['sass:server', 'autoprefixer']
      },
     
      styles: {
        files: ['css/{,*/}*.css'],
        tasks: ['newer:copy:styles', 'autoprefixer']
      },
      handlebars: {
        files: ['templates/*.hbs'],
        tasks: ['handlebars:compile']
      },
      livereload: {
        options: {
          livereload: '<%= connect.options.livereload %>'
        },
        files: [
          './{,*/}*.html',
          './css/{,*/}*.css',
          './images/{,*/}*',
        './sass/{,*/}*.{scss,sass}'
        ]
      }

    },

    sass: {
      server: {
        files: [{
          expand: true,
          cwd: 'sass',
          src: ['{,*/}*.{scss,sass}'],
          dest: '.tmp/css',
          ext: '.css'
        }]
      },
      dist: {
        files: [{
          expand: true,
          cwd: 'sass',
          src: ['{,*/}*.{scss,sass}'],
          dest: 'dist/css',
          ext: '.css'
        }]
      }
      
    },
     autoprefixer: {
            options: {
                browsers: ['Android > 20', 'iOS 5', 'Chrome > 5%', 'Safari > 5%','Opera > 5%']
            },
            serve: {
                files: [{
                    expand: true,
                    cwd: '.tmp/css/',
                    src: '{,*/}*.css',
                    dest: '.tmp/css/'
                }, {
                    expand: true,
                    cwd: './css/',
                    src: '{,*/}*.css',
                    dest: './css/'
                }]
            },
            dist: {
                files: [{
                    expand: true,
                    cwd: 'dist/css/',
                    src: '{,*/}*.css',
                    dest: 'dist/css/'
                }]
            }
        },




    rev: {
      dist: {
        files: {
          src: [
            '<%= config.dist %>/scripts/{,*/}*.js',
            '<%= config.dist %>/css/{,*/}*.css',
            '<%= config.dist %>/images/{,*/}*.*',
            '<%= config.dist %>/styles/fonts/{,*/}*.*',
            '<%= config.dist %>/*.{ico,png}'
          ]
        }
      }
    },

    useminPrepare: {
      html: 'index.html',
      options: {
        dest: 'dist',
        flow: {
          html: {
            steps: {
              js: ['concat', 'uglify'],
              css: ['cssmin']
            },
            post: {}
          }
        }
      }
    },

    // Performs rewrites based on filerev and the useminPrepare configuration
    usemin: {
      html: ['dist/index.html'],
      options: {
        assetsDirs: [
          'dist',
        ]
      }
    },
    connect: {
      options: {
        port: 9000,
        open: true,
        livereload: 35729,
        hostname: 'localhost',

      },
      livereload: {
        options: {
          middleware: function(connect) {
            return [
              connect.static('.tmp'),
              connect().use('js/lib', connect.static('js/lib')),
              connect.static('./')
            ]

          }
        }
      },
      dist: {
        options: {
          base: '<%= config.dist %>',
          livereload: false
        }
      }
    },

    concurrent: {
      server: [
        'sass:server',
        // 'handlebars:compile'
      ],
      dist: [
        'sass:dist',
        'handlebars:dist',
        // 'copy:dist'
      ]
      
    }



  });

  grunt.registerTask('serve', 'start the server and preview your app, --allow-remote for remote access', function(target) {
    if (grunt.option('allow-remote')) {
      grunt.config.set('connect.options.hostname', '10.100.1.87');
    }
    if (target === 'dist') {
      return grunt.task.run(['build', 'connect:dist:keepalive']);
    }

    grunt.task.run([
      // 'clean:server',
      'concurrent:server',
      'connect:livereload',
      'autoprefixer',
      'watch',
      'sass:dist',

    ]);
  });

  grunt.registerTask('server', function(target) {
    grunt.log.warn('The `server` task has been deprecated. Use `grunt serve` to start a server.');
    grunt.task.run([target ? ('serve:' + target) : 'serve']);
  });

  grunt.registerTask('build', [
    'clean',
    'useminPrepare',
    'concat',
    'cssmin',
    'uglify',
    'copy:dist',
    // 'imagemin',
    // 'filerev',
    'usemin',
    'autoprefixer'

  ]);
}